<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="订单号" prop="orderNo">
      <el-input v-model="dataForm.orderNo" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="产品id" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="产品id"></el-input>
    </el-form-item>
    <el-form-item label="购买时间" prop="purchaseDate">
      <el-input v-model="dataForm.purchaseDate" placeholder="购买时间"></el-input>
    </el-form-item>
    <el-form-item label="" prop="transactionId">
      <el-input v-model="dataForm.transactionId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="app包全名" prop="bundleId">
      <el-input v-model="dataForm.bundleId" placeholder="app包全名"></el-input>
    </el-form-item>
    <el-form-item label="app版本" prop="applicationVersion">
      <el-input v-model="dataForm.applicationVersion" placeholder="app版本"></el-input>
    </el-form-item>
    <el-form-item label="" prop="inAppOwnershipType">
      <el-input v-model="dataForm.inAppOwnershipType" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="价格：单位分" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格：单位分"></el-input>
    </el-form-item>
    <el-form-item label="购买月数" prop="months">
      <el-input v-model="dataForm.months" placeholder="购买月数"></el-input>
    </el-form-item>
    <el-form-item label="交易类型0 购买vip记录，1主题，2组件" prop="type">
      <el-input v-model="dataForm.type" placeholder="交易类型0 购买vip记录，1主题，2组件"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="购买时间" prop="created">
      <el-input v-model="dataForm.created" placeholder="购买时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updated">
      <el-input v-model="dataForm.updated" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          orderNo: '',
          productId: '',
          purchaseDate: '',
          transactionId: '',
          bundleId: '',
          applicationVersion: '',
          inAppOwnershipType: '',
          price: '',
          months: '',
          type: '',
          status: '',
          created: '',
          updated: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          orderNo: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '产品id不能为空', trigger: 'blur' }
          ],
          purchaseDate: [
            { required: true, message: '购买时间不能为空', trigger: 'blur' }
          ],
          transactionId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          bundleId: [
            { required: true, message: 'app包全名不能为空', trigger: 'blur' }
          ],
          applicationVersion: [
            { required: true, message: 'app版本不能为空', trigger: 'blur' }
          ],
          inAppOwnershipType: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格：单位分不能为空', trigger: 'blur' }
          ],
          months: [
            { required: true, message: '购买月数不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '交易类型0 购买vip记录，1主题，2组件不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          created: [
            { required: true, message: '购买时间不能为空', trigger: 'blur' }
          ],
          updated: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/trade/user-order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.userOrder.userId
                this.dataForm.orderNo = data.userOrder.orderNo
                this.dataForm.productId = data.userOrder.productId
                this.dataForm.purchaseDate = data.userOrder.purchaseDate
                this.dataForm.transactionId = data.userOrder.transactionId
                this.dataForm.bundleId = data.userOrder.bundleId
                this.dataForm.applicationVersion = data.userOrder.applicationVersion
                this.dataForm.inAppOwnershipType = data.userOrder.inAppOwnershipType
                this.dataForm.price = data.userOrder.price
                this.dataForm.months = data.userOrder.months
                this.dataForm.type = data.userOrder.type
                this.dataForm.status = data.userOrder.status
                this.dataForm.created = data.userOrder.created
                this.dataForm.updated = data.userOrder.updated
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/trade/user-order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'orderNo': this.dataForm.orderNo,
                'productId': this.dataForm.productId,
                'purchaseDate': this.dataForm.purchaseDate,
                'transactionId': this.dataForm.transactionId,
                'bundleId': this.dataForm.bundleId,
                'applicationVersion': this.dataForm.applicationVersion,
                'inAppOwnershipType': this.dataForm.inAppOwnershipType,
                'price': this.dataForm.price,
                'months': this.dataForm.months,
                'type': this.dataForm.type,
                'status': this.dataForm.status,
                'created': this.dataForm.created,
                'updated': this.dataForm.updated
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
